# Author: Wu Zhangjin / Falcon <falcon@tinylab.org>
# Update: 2023/05/03 Wednesday 17:40:38 CST

SRC = $(wildcard *.md)

html_theme=dzslides
latex_theme=Darmstadt

# colortheme: beaver,crane,dolphin,dove,fly,lily
# monarca,seagull,seahorse,spruce,wolverine
latex_colortheme=wolverine

# configure the ratio of width/height
latex_aspectratio=169

# Put and select the .jpg's in images/, without suffix
# e.g. earth, white, darkgray; mx6, orange, black
titlepage_wallpaper=earth
titlepage_titlefg=white
titlepage_authorfg=darkgray

# toc bg/fg
toc_number_bg=white
toc_number_fg=violet
toc_section_bg=white
toc_section_fg=orange

# link-color
# xcolor: https://www.overleaf.com/learn/latex/Using_colours_in_LaTeX
LINK_COLOR ?= Cyan

# encryption: empty to disable, nonempty to enable
ENCRYPT ?=
OWNERPW ?= $(ENCRYPT)
USERPW  ?= $(ENCRYPT)
ifneq ($(OWNERPW)$(USERPW), $(ENCRYPT))
  ENCRYPT := $(OWNERPW)$(USERPW)
endif

latex_fontsize=9pt
latex_template=./templates/zh_template.tex

# pygments / kate /monochrome /espresso /haddock /tango /zenburn
highlight_style ?= espresso

PDFS=$(SRC:.md=.pdf)
HTML=$(SRC:.md=.html)
IMGS=$(wildcard images/*.jpg)

CJKmainfont = AR PL SungtiL GB
mainfont = AR PL SungtiL GB

all: clean $(PDFS) $(HTML)

pdf: $(PDFS)

html: $(HTML)

%.pdf: %.md $(latex_template) $(IMGS)
	sed '/^# 致谢/,$$d' $< > $<.tmp
	pandoc -t beamer --toc --listings \
	  --latex-engine=xelatex \
	  --template=$(latex_template) \
          $(if $(latex_aspectratio),-V aspectratio=$(latex_aspectratio)) \
	  -V titlepage_wallpaper:$(titlepage_wallpaper) \
	  -V titlepage_titlefg:$(titlepage_titlefg) \
	  -V titlepage_authorfg:$(titlepage_authorfg) \
	  -V toc_number_bg:$(toc_number_bg) \
	  -V toc_number_fg:$(toc_number_fg) \
	  -V toc_section_bg:$(toc_section_bg) \
	  -V toc_section_fg:$(toc_section_fg) \
	  -V colortheme:$(latex_colortheme) \
	  -V theme:$(latex_theme) \
	  -V fontsize:$(latex_fontsize) \
	  -V CJKmainfont:'$(CJKmainfont)'\
	  -V mainfont:'$(mainfont)'\
	  --highlight-style $(highlight_style)\
	  -V colorlinks -V urlcolor:$(LINK_COLOR) \
	  $(if $(ENCRYPT),-V encrypt:$(ENCRYPT) -V ownerpw:$(OWNERPW) -V userpw:$(USERPW)) \
	  $(if $(WATERMARK),-V watermark:$(WATERMARK)) \
	  $<.tmp -o $@
	rm $<.tmp

%.html: %.md
	pandoc -t $(html_theme) -s --mathjax $< -o $@
	sed -i -e "s/width: 800px; height: 600px;$$/width: 1280px; height: 768px;/" $@
	sed -i -e "s/margin-left: -400px; margin-top: -300px;$$/margin-left: -640px; margin-top: -384px;/" $@

read: $(PDFS)
	(chromium-browser $^ 2>&1 > /dev/null &) 2>&1 >/dev/null

view:
	evince $(PDFS)

read-html: $(HTML)
	(chromium-browser $^ 2>&1 > /dev/null &) 2>&1 >/dev/null

clean:
	rm -rf $(PDFS) $(HTML) *.tmp
